package com.course.business.controller.admin;

import com.course.business.controller.BaseController;
import com.course.model.dto.*;
import com.course.common.response.BaseResponse;
import com.course.model.service.CourseCategoryService;
import com.course.model.service.CourseService;
import com.course.model.util.ValidatorUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/course")
public class CourseController extends BaseController {

    public static final String BUSINESS_NAME = "课程";

    @Resource
    private CourseService courseService;

    @Resource
    private CourseCategoryService courseCategoryService;

    /**
     * 列表查询
     */
    @PostMapping("/getCourseList")
    public BaseResponse list(@RequestBody PageDto pageDto) {
        BaseResponse response = new BaseResponse().sucess();
        courseService.list(pageDto);
        response.setData(pageDto);
        return response;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public BaseResponse save(@RequestBody CourseDto courseDto) {
        // 保存校验
        ValidatorUtil.require(courseDto.getName(), "名称");
        ValidatorUtil.length(courseDto.getName(), "名称", 1, 50);
        ValidatorUtil.length(courseDto.getSummary(), "概述", 1, 2000);
        ValidatorUtil.length(courseDto.getImage(), "封面", 1, 100);

        BaseResponse response = new BaseResponse().sucess();
        courseService.save(courseDto);
        response.setData(courseDto);
        return response;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public BaseResponse delete(@PathVariable String id) {
        BaseResponse response = new BaseResponse().sucess();
        courseService.delete(id);
        return response;
    }

    /**
     * 查找课程下所有分类
     *
     * @param courseId
     */
    @PostMapping("/list-category/{courseId}")
    public BaseResponse listCategory(@PathVariable(value = "courseId") String courseId) {
        BaseResponse response = new BaseResponse().sucess();
        response.setData(courseCategoryService.listByCourse(courseId));
        return response;
    }

    /**
     * 查询课程内容
     *
     * @param id
     * @return
     */
    @GetMapping("/find-content/{id}")
    public BaseResponse findContent(@PathVariable String id) {
        BaseResponse response = new BaseResponse().sucess();
        response.setData(courseService.findContent(id));
        return response;
    }

    /**
     * 保存课程内容
     *
     * @param contentDto
     * @return
     */
    @PostMapping("/save-content")
    public BaseResponse saveContent(@RequestBody CourseContentDto contentDto) {
        BaseResponse response = new BaseResponse().sucess();
        response.setData(courseService.saveContent(contentDto));
        return response;
    }

    /**
     * 更新排序
     * @param sortDto
     * @return
     */
    @PostMapping(value = "/sort")
    public BaseResponse updateSort(@RequestBody SortDto sortDto) {
        logger.info("更新排序");
        BaseResponse response = new BaseResponse().sucess();
        courseService.sort(sortDto);
        response.setData(sortDto);
        return response;
    }
}
